package com.study.controller.user;

import com.study.dto.OrderSubmitDTO;
import com.study.dto.OrdersPageQueryDTO;
import com.study.dto.UserCancelOrderDTO;
import com.study.result.PageResult;
import com.study.result.Result;
import com.study.service.OrderService;
import com.study.vo.OrderSubmitVO;
import com.study.vo.OrdersDetailVO;
import com.study.vo.UserOrderDataVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 用户页面订单数据获取
     * @param userId
     * @return
     */
    @GetMapping("/userOrderData/{userId}")
    @ApiOperation(value = "用户页面订单数据获取")
    public Result<UserOrderDataVO> userOrderData(@PathVariable Long userId){
        UserOrderDataVO userOrderDataVO =orderService.getUserOrderData(userId);
        return Result.success(userOrderDataVO);
    }

    //用户下单
    @PostMapping("/placeOrder")
    @ApiOperation(value = "用户下单")
    public Result<OrderSubmitVO> placeOrder(@RequestBody OrderSubmitDTO orderSubmitDTO){
        OrderSubmitVO orderSubmitVO = orderService.placeOrder(orderSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 根据状态获取用户历史订单列表
     * @param OrdersPageQueryDTO
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation(value = "根据状态获取用户历史订单列表")
    public Result<PageResult> getOrderListByStatus(OrdersPageQueryDTO OrdersPageQueryDTO){
        PageResult pageResult = orderService.getOrderListByStatus(OrdersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 支付订单
     * @param id
     * @return
     */
    @GetMapping("/payment/{id}")
    @ApiOperation(value = "支付订单")
    public Result payment(@PathVariable Long id){
        orderService.payment(id);
        return Result.success("支付成功");
    }

    /**
     * 取消订单
     * @param userCancelOrderDTO
     * @return
     */
    @PostMapping("/userCancel")
    @ApiOperation(value = "取消订单")
    public Result cancel(@RequestBody UserCancelOrderDTO userCancelOrderDTO){
        orderService.cancel(userCancelOrderDTO);
        return Result.success("订单取消成功");
    }

    /**
     * 获取订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation(value = "获取订单详情")
    public Result<OrdersDetailVO> orderDetail(@PathVariable Long id) {
        OrdersDetailVO ordersDetail = orderService.getOrderById(id);
        return Result.success(ordersDetail);
    }
}
